package model

import (
	"time"

	"gorm.io/gorm"
)

type User struct {
	Id         int64     `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
	Number     string    `gorm:"column:number;type:varchar(255);comment:学号;NOT NULL" json:"number"`
	Name       string    `gorm:"column:name;type:varchar(255);comment:用户名称;NOT NULL" json:"name"`
	Password   string    `gorm:"column:password;type:varchar(255);comment:用户密码;NOT NULL" json:"password"`
	Gender     string    `gorm:"column:gender;type:char(5);comment:男｜女｜未公开;NOT NULL" json:"gender"`
	CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP" json:"create_time"`
	UpdateTime time.Time `gorm:"column:update_time;type:timestamp;default:CURRENT_TIMESTAMP" json:"update_time"`
}

func (m *User) TableName() string {
	return "user"
}

func (m *User) First(db *gorm.DB) error {
	if err := db.First(&m).Error; err != nil {
		return err
	}
	return nil
}

func (m *User) Add(db *gorm.DB) error {
	db = db.Model(&m)
	if err := db.Create(&m).Error; err != nil {
		return err
	}
	return nil
}

func (m *User) List(db *gorm.DB) ([]*User, error) {
	db = db.Model(&m)
	var list []*User
	if err := db.Find(&list).Error; err != nil {
		return list, err
	}
	return list, nil
}
